import cmath
import math
import threading
import turtle
import turtle as t
import xlrd
from PIL import Image


def rectangle_blue(value1, value2, value3, value4, value5, total_score, number, name):
    namecolor = "black"
    t.penup()
    #不考虑平均值那一行的情况
    if (sheet1.nrows-1)/show_rows_input > (sheet1.nrows-1)//show_rows_input:
        show_rows =(sheet1.nrows - 1) // show_rows_input + 1
    else:
        show_rows =(sheet1.nrows - 1) / show_rows_input

    if number <= show_rows:
        t.goto(230 * (number - 1) - 900, 300)
    elif number <= show_rows*2:
        t.goto(230 * (number - show_rows-1) - 900, 70)
    elif number <= show_rows*3:
        t.goto(230 * (number-show_rows*2 - 1) - 900, -160)
    else:
        t.goto(230 * (number-show_rows*3 - 1) - 900, -390)
    t.pendown()

    if total_score >= 80 and number <= sheet1.nrows - 3:
        background_color = "#0000D1"
        namecolor = "white"
    elif total_score >= 80 and number == sheet1.nrows - 2:
        background_color = "green"
    elif total_score >= 80 and number == sheet1.nrows - 1:
        background_color = "yellow"
    else:
        background_color = "red"
    if total_score == sheet1.row(sheet1.nrows - 1)[6].value and total_score >= 80:
        background_color = "yellow"
    if total_score == sheet1.row(sheet1.nrows - 1)[6].value and total_score < 80:
        background_color = "red"
    t.speed(1000)
    t.penup()
    t.color("white")
    t.fd(70)
    t.pendown()
    t.color(background_color)
    t.begin_fill()
    t.fd(60)
    if value1 > 90:
        t.fd(70)
        t.left(90)
        t.fd(70)
    else:
        t.left(45)
        t.fd(math.sqrt(9800))
        t.left(45)
    t.fd(60)
    if value2 > 90:
        t.fd(70)
        t.left(90)
        t.fd(70)
    else:
        t.left(45)
        t.fd(math.sqrt(9800))
        t.left(45)
    t.fd(60)
    if value3 > 90:
        t.fd(70)
        t.left(90)
        t.fd(70)
    else:
        t.left(45)
        t.fd(math.sqrt(9800))
        t.left(45)
    t.fd(60)
    if value4 > 90:
        t.fd(70)
        t.left(90)
        t.fd(70)
    else:
        t.left(45)
        t.fd(math.sqrt(9800))
        t.left(45)
    t.end_fill()
    # 写名字
    t.color(namecolor)
    turtle.write(name, False, "left", font=("arial", 14, "normal"))
    # 画圆
    t.color(background_color)
    t.penup()
    #圆的变成扩大到40
    t.fd(70)
    t.left(90)
    t.fd(100)
    t.pendown()
    if value5 > 95:
        circle_color = "#0000D1"
    elif value5 > 85:
        circle_color = "green"
    elif value5 > 75:
        circle_color = "yellow"
    else:
        circle_color = "red"
    t.pen(fillcolor=circle_color, pencolor="white", pensize=3)
    t.begin_fill()
    t.circle(40)
    t.end_fill()
    # 写智慧港口所和总分数
    t.pen(fillcolor=background_color, pencolor=background_color, pensize=1)
    t.penup()
    t.fd(60)
    t.left(90)
    t.fd(40)
    t.right(90)
    t.pendown()
    t.color(namecolor)
    turtle.write(total_score, False, "center", font=("arial", 14, "normal"))
    t.color(background_color)
    t.penup()
    t.fd(20)
    t.pendown()
    t.color(namecolor)
    turtle.write("智慧院", False, "center", font=("arial", 14, "normal"))
    t.color(background_color)

    t.penup()
    t.fd(21)
    t.color("white")
    t.left(90)
    t.fd(100)
    t.left(90)
    t.fd(201)
    t.left(90)
    t.pendown()


def mainWork():
    t.title('ManagerPortrait')
    t.screensize(5000,2000)
    i = 1
    while i < sheet1.nrows:
        # 取本行指令的操作类型
        name = sheet1.row(i)[0].value
        value1 = sheet1.row(i)[1].value
        value2 = sheet1.row(i)[2].value
        value3 = sheet1.row(i)[3].value
        value4 = sheet1.row(i)[4].value
        value5 = sheet1.row(i)[5].value
        total_score = sheet1.row(i)[6].value
        number = sheet1.row(i)[7].value

        rectangle_blue(value1, value2, value3, value4, value5, total_score, number, name)
        i += 1


if __name__ == '__main__':
    file = 'manager.xls'
    # 打开文件
    wb = xlrd.open_workbook(filename=file)
    # 通过索引获取表格sheet页
    sheet1 = wb.sheet_by_index(0)
    show_rows_input = int(input("请问生成3行还是4行？"))
    mainWork()


    t.done()
